//
// Created by konglinglong on 2020/9/15.
//

#ifndef CSP_INCLUDE_HWAL_MISC_H_
#define CSP_INCLUDE_HWAL_MISC_H_

#include "commDef.h"

#if defined(__i386__)
static inline unsigned long long CSP_rdtsc(void) __attribute__((always_inline));
static inline unsigned long long CSP_rdtsc(void) {
  unsigned long long int x;
  __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
  return x;
}
#elif defined(__x86_64__)
static inline unsigned long long CSP_rdtsc(void) __attribute__((always_inline));
static inline unsigned long long CSP_rdtsc(void) {
    unsigned long long a, d;
    __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));
    return (d<<32) | a;
}
#elif defined(__arm__)
static inline uint32_t CSP_rdtsc(void) __attribute__((always_inline));
static inline uint32_t CSP_rdtsc(void) {
  uint32_t r = 0;
  asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(r) );
  return r;
}
#endif

#endif //CSP_INCLUDE_HWAL_MISC_H_
